import util from '@/utils/utils'

const formatOpt = (opt = {}) => {
    opt.pageNum = opt.page
    opt.pageSize = opt.rows
    return opt
}
export default axios => {
    return {
        uploadFile(file) {
            let formData = new FormData()
            formData.append('file', file)
            return axios.post('', formData)
        },
        wx: {
            pay(orderNum) {
                const openid = util.store.get('userInfo').gzhOpenid
                return axios.get('', {params: {orderNum, openid}})
            },
            sdk(url) {
                return axios.post(``, {url})
            }
        },
        news: {
            list(options = {}) {
                return axios.get('', {params: formatOpt(options)})
            },
            detail(iD) {
                return axios.get('', {params: {iD}})
            },
        },
        // 问题
        qa: {
            list(options = {}) {
                return axios.get('', {params: formatOpt(options)})
            },
            consult(options = {}) {
                return axios.post('', options)
            },
            consultList(options = {}) {
                return axios.get('', {params: formatOpt(options)})
            },
            validateCode() {
                return axios.get('', {
                    responseType: 'arraybuffer'
                }).then(res => {
                    return 'data:image/png;base64,' + btoa(
                        new Uint8Array(res).reduce((data, byte) => data + String.fromCharCode(byte), '')
                    )
                })
            }
        },
        order: {
            list(options = {}) {
                return axios.post('', options)
            },
            detail(appointId) {
                return axios.post('', {appointId})
            },
            qrCode(appointId) {
                return axios.post('', {appointId})
            },
            refund(appointId) {
                return axios.post('', {appointId})
            },
            cancel(appointId) {
                return axios.post('', {appointId})
            }
        },
        // 商品
        goods: {
            list(options = {}) {
                return axios.post('', options)
            },
            detail(id) {
                return axios.post('', {id})
            },
            submit(options = {}) {
                return axios.post('', options)
            },
            pay(orderId, channel, userId) {
                return axios.post('', {orderId, channel, userId})
            },
        },
        user: {
            sendMessage(phone) {
                return axios.get('', {params: {phone}})
            },
            register(loginName, verificationCode, password) {
                return axios.post('', {loginName, verificationCode, password})
            },
            resetPassword(username, verificationCode, password) {
                return axios.post('', {username, verificationCode, password})
            },
            login(username, password) {
                return axios.post('/login', {username, password})
            },
            info() {
                return axios.get('')
            },
            uploadAvatar(file) {
                let formData = new FormData()
                formData.append('avatarfile', file)
                return axios.post('', formData)
            },
            updateUserInfo(options = {}) {
                return axios.post('', options)
            },
            bindPhone(options = {}) {
                return axios.post('', options)
            },
            bindEmail(options = {}) {
                return axios.post('', options)
            },
        },
    }
}
